package com.atguigu.yygh.orders.receiver;

import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.orders.service.OrderInfoService;
import com.atguigu.yygh.rabbit.RabbitService;
import com.atguigu.yygh.rabbit.constant.MqConst;
import com.atguigu.yygh.vo.msm.MsmVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rabbitmq.client.Channel;
import org.joda.time.DateTime;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

@Component
public class OrderReceiver {
    @Autowired
    OrderInfoService orderInfoService;
    @Autowired
    RabbitService rabbitService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(MqConst.QUEUE_TASK_8),
            exchange = @Exchange(MqConst.EXCHANGE_DIRECT_TASK),
            key = MqConst.ROUTING_TASK_8
    ))
    public void test(String date, Message message, Channel channel) {
        // 拿到明天的日期，订单表中与明天日期相等的条数，证明明天患者看病日期
        // 状态得是0才提示
        LambdaQueryWrapper<OrderInfo> lambdaQueryWrapper = new LambdaQueryWrapper<OrderInfo>();
        lambdaQueryWrapper.eq(OrderInfo::getReserveDate, new DateTime(date).toDate());
        lambdaQueryWrapper.eq(OrderInfo::getOrderStatus, OrderStatusEnum.UNPAID);
        List<OrderInfo> list = orderInfoService.list(lambdaQueryWrapper);

        list.forEach((orderInfo) -> {
            System.out.println("进入循环，处理订单ID：" + orderInfo.getId()); // 新增日志
            MsmVo msmVo = new MsmVo();
            msmVo.setPhone(orderInfo.getPatientPhone());
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_MSM, MqConst.ROUTING_MSM_ITEM, msmVo);
        });

    }
}
